home *** CD-ROM | disk | FTP | other *** search
/ PC Media 23 / PC MEDIA CD23.iso / share / prog / newmat / tmt.cpp < prev    next >
C/C++ Source or Header  |  1995-01-17  |  6KB  |  211 lines

  1. #define WANT_STREAM
  2.  
  3. #include "include.h"
  4.  
  5. #include "newmat.h"
  6.  
  7. /**************************** test program ******************************/
  8.  
  9. class PrintCounter
  10. {
  11.    int count;
  12.    char* s;
  13. public:
  14.    ~PrintCounter();
  15.    PrintCounter(char * sx) : count(0), s(sx) {}
  16.    void operator++() { count++; }
  17. };
  18.  
  19. PrintCounter PCZ("Number of non-zero matrices (should be 1) = ");
  20. PrintCounter PCN("Number of matrices tested                 = ");
  21.  
  22. PrintCounter::~PrintCounter()
  23. { cout << s << count << "\n"; }
  24.  
  25.  
  26. void Print(const Matrix& X)
  27. {
  28.    ++PCN;
  29.    cout << "\nMatrix type: " << X.Type().Value() << " (";
  30.    cout << X.Nrows() << ", ";
  31.    cout << X.Ncols() << ")\n\n";
  32.    if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
  33.    int nr=X.Nrows(); int nc=X.Ncols();
  34.    for (int i=1; i<=nr; i++)
  35.    {
  36.       for (int j=1; j<=nc; j++)  cout << X(i,j) << "\t";
  37.       cout << "\n";
  38.    }
  39.    cout << flush; ++PCZ;
  40. }
  41.  
  42. void Print(const UpperTriangularMatrix& X)
  43. {
  44.    ++PCN;
  45.    cout << "\nMatrix type: " << X.Type().Value() << " (";
  46.    cout << X.Nrows() << ", ";
  47.    cout << X.Ncols() << ")\n\n";
  48.    if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
  49.    int nr=X.Nrows(); int nc=X.Ncols();
  50.    for (int i=1; i<=nr; i++)
  51.    {
  52.       for (int j=1; j<i; j++) cout << "\t";
  53.       for (j=i; j<=nc; j++)  cout << X(i,j) << "\t";
  54.       cout << "\n";
  55.    }
  56.    cout << flush; ++PCZ;
  57. }
  58.  
  59. void Print(const DiagonalMatrix& X)
  60. {
  61.    ++PCN;
  62.    cout << "\nMatrix type: " << X.Type().Value() << " (";
  63.    cout << X.Nrows() << ", ";
  64.    cout << X.Ncols() << ")\n\n";
  65.    if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
  66.    int nr=X.Nrows(); int nc=X.Ncols();
  67.    for (int i=1; i<=nr; i++)
  68.    {
  69.       for (int j=1; j<i; j++) cout << "\t";
  70.       if (i<=nc) cout << X(i,i) << "\t";
  71.       cout << "\n";
  72.    }
  73.    cout << flush; ++PCZ;
  74. }
  75.  
  76. void Print(const SymmetricMatrix& X)
  77. {
  78.    ++PCN;
  79.    cout << "\nMatrix type: " << X.Type().Value() << " (";
  80.    cout << X.Nrows() << ", ";
  81.    cout << X.Ncols() << ")\n\n";
  82.    if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
  83.    int nr=X.Nrows(); int nc=X.Ncols();
  84.    for (int i=1; i<=nr; i++)
  85.    {
  86.       for (int j=1; j<i; j++) cout << X(j,i) << "\t";
  87.       for (j=i; j<=nc; j++)  cout << X(i,j) << "\t";
  88.       cout << "\n";
  89.    }
  90.    cout << flush; ++PCZ;
  91. }
  92.  
  93. void Print(const LowerTriangularMatrix& X)
  94. {
  95.    ++PCN;
  96.    cout << "\nMatrix type: " << X.Type().Value() << " (";
  97.    cout << X.Nrows() << ", ";
  98.    cout << X.Ncols() << ")\n\n";
  99.    if (X.IsZero()) { cout << "All elements are zero\n" << flush; return; }
  100.    int nr=X.Nrows();
  101.    for (int i=1; i<=nr; i++)
  102.    {
  103.       for (int j=1; j<=i; j++) cout << X(i,j) << "\t";
  104.       cout << "\n";
  105.    }
  106.    cout << flush; ++PCZ;
  107. }
  108.  
  109.  
  110. void Clean(Matrix& A, Real c)
  111. {
  112.    int nr = A.Nrows(); int nc = A.Ncols();
  113.    for (int i=1; i<=nr; i++)
  114.    {
  115.       for ( int j=1; j<=nc; j++)
  116.       { Real a = A(i,j); if ((a < c) && (a > -c)) A(i,j) = 0.0; }
  117.    }
  118. }
  119.  
  120. void Clean(DiagonalMatrix& A, Real c)
  121. {
  122.    int nr = A.Nrows();
  123.    for (int i=1; i<=nr; i++)
  124.    { Real a = A(i,i); if ((a < c) && (a > -c)) A(i,i) = 0.0; }
  125. }
  126.  
  127. void PentiumCheck(Real N, Real D)
  128. {
  129.    Real R = N / D;
  130.    R = R * D - N;
  131.    if ( R > 1 || R < -1)
  132.       cout << "Pentium error detected: % error = " << 100 * R / N << "\n";
  133. }
  134.  
  135.  
  136. void trymat1(); void trymat2(); void trymat3();
  137. void trymat4(); void trymat5(); void trymat6();
  138. void trymat7(); void trymat8(); void trymat9();
  139. void trymata(); void trymatb(); void trymatc();
  140. void trymatd(); void trymate(); void trymatf();
  141. void trymatg(); void trymath(); void trymati();
  142. void trymatj();
  143.  
  144. main()
  145. {
  146.    Real* s1; Real* s2; Real* s3; Real* s4;
  147.    cout << "\nBegin test\n";   // Forces cout to allocate memory at beginning
  148.    { Matrix A1(40,200); s1 = A1.Store(); }
  149.    { Matrix A1(1,1); s3 = A1.Store(); }
  150.    {
  151.       Tracer et("Matrix test program");
  152.  
  153.       Matrix A(25,150);
  154.       {
  155.      int i;
  156.      RowVector A(8);
  157.      for (i=1;i<=7;i++) A(i)=0.0; A(8)=1.0;
  158.      Print(A);
  159.       }
  160.       cout << "\n";
  161.  
  162.       TestTypeAdd(); TestTypeMult(); TestTypeSP(); TestTypeOrder();
  163.  
  164.       Try { 
  165.  
  166.          trymat1();
  167.          trymat2();
  168.          trymat3();
  169.          trymat4();
  170.          trymat5();
  171.          trymat6();
  172.          trymat7();
  173.          trymat8();
  174.          trymat9();
  175.          trymata();
  176.          trymatb();
  177.          trymatc();
  178.          trymatd();
  179.          trymate();
  180.          trymatf();
  181.          trymatg();
  182.          trymath();
  183.          trymati();
  184.          trymatj();
  185.  
  186.          cout << "\nEnd of tests\n";
  187.       }
  188.       CatchAll { cout << "\nException generated in test program\n\n"; }
  189.    }
  190.  
  191.    { Matrix A1(40,200); s2 = A1.Store(); }
  192.    cout << "\n(The following memory checks are probably not valid with all\n";
  193.    cout << "compilers - see documentation)\n";
  194.    cout << "\nChecking for lost memory: "
  195.       << (unsigned long)s1 << " " << (unsigned long)s2 << " ";
  196.    if (s1 != s2) cout << " - error\n"; else cout << " - ok\n\n";
  197.    { Matrix A1(1,1); s4 = A1.Store(); }
  198.    cout << "\nChecking for lost memory: "
  199.       << (unsigned long)s3 << " " << (unsigned long)s4 << " ";
  200.    if (s3 != s4) cout << " - error\n"; else cout << " - ok\n\n";
  201.  
  202.    // check for Pentium bug
  203.    PentiumCheck(4195835L,3145727L);
  204.    PentiumCheck(5244795L,3932159L);
  205.  
  206. #ifdef DO_FREE_CHECK
  207.    FreeCheck::Status();
  208. #endif 
  209.    return 0;
  210. }
  211.